requirejs.config({
    paths: {
        vue: "backend/shopro/common/vue"
    }
})
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'vue'], function ($, undefined, Backend, Table, Form, Vue) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shopro/order/order/index' + location.search,
                    add_url: 'shopro/order/order/add',
                    edit_url: 'shopro/order/order/edit',
                    del_url: 'shopro/order/order/del',
                    multi_url: 'shopro/order/order/multi',
                    table: 'shopro_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'type',
                            title: __('Type'),
                            searchList: {"goods": __('Type goods'), "score": __('Type score')},
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'order_sn', title: __('Order_sn')},
                        {
                            field: 'goodsInfo', title: '商品信息',
                            formatter: function (value, row) {
                                let goodsStr = '';
                                row.item.forEach(i => {
                                    goodsStr += `<div style="display: flex;margin: 5px 0;width:260px"><image style="    width: 50px;
                                    height: 50px;margin-right:10px" src="${i.goods_image}"></image><div style="    display: flex;
                                    flex-direction: column;
                                    justify-content: space-between;"><div style="text-align:left;overflow : hidden;
                                    text-overflow: ellipsis;
                                    display: -webkit-box;
                                    -webkit-line-clamp: 2;
                                    -webkit-box-orient: vertical;
                                    width:200px;">${i.goods_title}</div><div style="text-align:left">x${i.goods_num}</div></div></div>`;
                                })
                                return goodsStr;
                            }

                        },
                        {field: 'user_id', title: __('User_id'),visible:false},
                        {
                            field: 'people', title: '收货人',
                            formatter: function (value, row) {
                                return `<div><span>${row.consignee}<br>${row.phone}<br>${row.province_name}-${row.city_name}-${row.area_name}<br>${row.address}</span></div>`
                            }
                        },

                        {field: 'remark', title: __('Remark')},

                        {field: 'total_amount', title: __('Total_amount'), operate: 'BETWEEN'},
                        {field: 'total_fee', title: __('Total_fee'), operate: 'BETWEEN'},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "-2": __('Status -2'),
                                "-1": __('Status -1'),
                                "0": __('Status 0'),
                                "1": __('Status 1'),
                                "2": __('Status 2')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'memo', title: __('Memo'),
                            formatter: function (value, row) {
                                return `<div>${value == null ? '' : value}<i data-id=${row.id} class="fa fa-edit testMemo info"></i></div>`
                            }
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: '订单管理',
                            table: table,
                            operate: false,
                            buttons: [
                                {
                                    name: 'status',
                                    title: '查看详情',
                                    text: '查看详情',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: function (row) {
                                        return 'shopro/order/order/detail?id=' + row.id
                                    },

                                    success: function (data, ret) {
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $(document).on("click", ".testMemo", function () {
                let id = $(this).attr("data-id")
                Layer.prompt({title: __('请输入商户备注'), formType: 0}, function (value, index) {
                    Backend.api.ajax({
                        url: "shopro/order/order/editMemo/id/" + id,
                        data: {
                            memo: value
                        },

                    }, function () {
                        table.bootstrapTable('refresh');
                        Layer.close(index);
                    });

                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shopro/order/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shopro/order/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shopro/order/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            var vue = new Vue({
                el: "#orderDetail",
                data() {
                    return {
                        orderDetail: Config.order,
                        orderGoodsList: Config.item,
                        orderFrom: {},
                        express_name: "",
                        express_no: "",
                        isexpress: true,
                        refund_money: null

                    }
                },
                filters: {
                    formatDate: function (value) {
                        let date = new Date(value * 1000);
                        let y = date.getFullYear();
                        let MM = date.getMonth() + 1;
                        MM = MM < 10 ? ('0' + MM) : MM;
                        let d = date.getDate();
                        d = d < 10 ? ('0' + d) : d;
                        let h = date.getHours();
                        h = h < 10 ? ('0' + h) : h;
                        let m = date.getMinutes();
                        m = m < 10 ? ('0' + m) : m;
                        let s = date.getSeconds();
                        s = s < 10 ? ('0' + s) : s;
                        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
                    },
                },
                mounted() {
                    this.isExpress()

                },
                methods: {
                    isExpress() {
                        var that = this;
                        that.orderGoodsList.forEach(item => {
                            if (item.dispatch_type == 'express') {
                                if (item.dispatch_status >= 1) {
                                    that.isexpress = false
                                }
                            } else {
                                that.isexpress = false
                            }
                        })
                    },
                    sendGoods(orderId, itemId, itemIdx) {
                        if (itemId == 0 && itemIdx == null) {
                            this.orderFrom.express_name = this.express_name
                            this.orderFrom.express_no = this.express_no
                        } else {
                            this.orderFrom.express_name = this.orderGoodsList[itemIdx].express_name
                            this.orderFrom.express_no = this.orderGoodsList[itemIdx].express_no
                        }

                        var that = this;
                        Fast.api.ajax({
                            url: `shopro/order/order/send/id/${orderId}/item_id/${itemId}`,
                            loading: true,
                            data: that.orderFrom

                        }, function (ret, res) {
                            that.orderGoodsList = []
                            res.data.forEach(item => {
                                that.orderGoodsList.push(item)
                            })
                            that.isExpress()
                        })
                    },
                    finishService(orderId, itemId) {
                        var that = this
                        Fast.api.ajax({
                            url: `shopro/order/order/aftersaleFinish/id/${orderId}/item_id/${itemId}`,
                            loading: true,
                            data: {}
                        }, function (ret, res) {
                            that.orderGoodsList = []
                            res.data.forEach(item => {
                                that.orderGoodsList.push(item)
                            })
                            that.isExpress()
                        })
                    },
                    goSelfPopover(idx) {
                        $(".selfPopoverCon").removeClass("selfPopoverShow")
                        $(".selfPopoverCon" + idx).addClass("selfPopoverShow")
                    },
                    popoverCancel(idx) {
                        $(".selfPopoverCon" + idx).removeClass("selfPopoverShow")
                    },
                    popoverReal(orderId, itemId, idx) {
                        $(".selfPopoverCon").removeClass("selfPopoverShow")
                        $(".selfPopoverCon" + idx).addClass("selfPopoverShow")
                        let num = this.refund_money
                        if (!num) {
                            alert("请输入退款金额")
                            return false
                        }
                        Fast.api.ajax({
                            url: `shopro/order/order/refund/id/${orderId}/item_id/${itemId}`,
                            loading: true,
                            data: {
                                refund_money: num
                            }
                        }, function (ret, res) {
                            vue.$data.orderGoodsList = []
                            res.data.forEach(item => {
                                vue.$data.orderGoodsList.push(item)
                            })
                            $(this).parent().prev().val("")
                        }, function () {

                        })
                    }
                },

            })
            //查看评价
            $(document).on("click", ".viewEvaluation", function () {
                let order_item_id = $(this).attr("data-id")
                parent.Fast.api.open("shopro/goods_comment/index?order_item_id=" + order_item_id, "查看评价", {
                    callback: function (data) {
                    }
                });
            })
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});